var uid,_datasDatagrid,editRowsNumber;

function addRow() {
	if (editRowsNumber != -1) {
		_datasDatagrid.datagrid('endEdit', editRowsNumber);
	} else {
		_datasDatagrid.datagrid('insertRow', {index: 0, row: {userId:uid}});
		_datasDatagrid.datagrid('beginEdit', 0);
		editRowsNumber = 0;
	}
};

function deleteRow() {
	var rows = _datasDatagrid.datagrid('getSelections');
	if (rows.length > 0) {
		$.messager.confirm('请确认', '您确实要删除当前所有选择的记录吗？', function(b){
			if (b) {
				var ids = [];
				for ( var i = 0; i < rows.length; i++) {
					ids.push(rows[i].id);
				}
				$.post(sy.bp()+"/admin/user/group/delete", {ids:ids.join(",")}, function(data) {
					if (data && data.result==1) {
						$.messager.show({msg:'用户组删除成功!',title:'系统提示'});
						editRowsNumber = -1;
						_datasDatagrid.datagrid('unselectAll');
						_datasDatagrid.datagrid('reload');
					} else {
						$.messager.alert('警告', data.msg, 'warning');
					}
				}, "json");
			}
		});
	} else {
		$.messager.alert('提示', '请选择要删除的记录', 'error');
	}
};

function saveRow() {
	_datasDatagrid.datagrid('endEdit', editRowsNumber);
}

function rejectRow() {
	editRowsNumber = -1;
	_datasDatagrid.datagrid('rejectChanges');
	_datasDatagrid.datagrid('unselectAll');
}

$(function() {
	uid = $('#uid').val();
	_datasDatagrid = $('#_datasDatagrid');
	editRowsNumber = -1;
	
	_datasDatagrid.datagrid({
		url : sy.bp()+'/admin/user/group/list?uid=' + uid,
		fit : true,
		autoRowHeight : true,
		striped : true,
		rownumbers : true,
		pagination : true,
		singleSelect : false,
		fitColumns : true,
		pageSize : 10,
		pageList : [10,20,30,40,50],
		nowrap : true,
		border : false,
		idField : 'id',
		sortName : 'createTime',
		sortOrder : 'desc',
		view: sy.myview,
        emptyMsg: '没有查询到相关数据',
		toolbar: [
		          {text:'增加',iconCls: 'icon-add',handler: addRow},'-',
		          {text:'删除',iconCls: 'icon-remove',handler: deleteRow},'-',
		          {text:'保存',iconCls: 'icon-save',handler: saveRow},'-',
		          {text:'撤销',iconCls: 'icon-redo',handler: rejectRow},'-',
		          {text:'刷新',iconCls: 'icon-reload',handler: function(){_datasDatagrid.datagrid('reload');}},'-'
		         ],
		frozenColumns: [[{field:'id',title:'编号',align:'center',width:100,sortable:true,checkbox:true}]],
		columns:[[
		          {field:'groupId',title:'组名称',algin:'center',width:100,
		        	  formatter:function(value,row,index){if (value) {return row.groupName}},
		        	  editor:{type:'combogrid',options:{
		      			align:'center',required:true,editable:false,panelWidth:600,panelHeight:300,idField:'id',textField:'name',
		      			url:sy.bp()+'/admin/group/findAll',sortName:'createTime',sortOrder:'desc',pageSize:100,pageList:[100],
		      			fitColumns:true,pagination:true,columns:[[
		      				{field:'id',title:'id',width:100,align:'center',hidden:true},
		      				{field:'name',title:'名称',width:300,align:'left',sortable:true},
		      				{field:'createTime',title:'创建时间',width:100,align:'center',sortable:true}
		                ]]
		        	  }}
		          },
		          {field:'groupName',title:'组名称',align:'left',width:100,hidden:true},
		          {field:'createTime',title:'创建时间',align:'left',width:100,sortable:true}
				]],
		onLoadSuccess : function(data) {
			editRowsNumber = -1;
			$.messager.progress('close');
		},
		onRowContextMenu : function(e, rowIndex, rowData){
			e.preventDefault();
			_datasDatagrid.datagrid('unselectAll').datagrid('selectRow', rowIndex);
			$('#datagridContextMenu').menu('show', {left:e.pageX,top:e.pageY});
		},
		onAfterEdit : function(rowIndex, rowData, changes) {
			var inserted = _datasDatagrid.datagrid('getChanges', 'inserted');
			var updated = _datasDatagrid.datagrid('getChanges', 'updated');
			if (inserted.length < 1 && updated.length < 1) {
				editRowsNumber = -1;
				_datasDatagrid.datagrid('unselectAll');
				return;
			}
			var url='';
			if (inserted.length > 0) {
				url=sy.bp()+'/admin/user/group/add';
			}
			if (updated.length > 0) {
				url=sy.bp()+'/admin/user/group/update';
			}
			$.post(url, rowData, function(data) {
				if (data && data.result==1) {
					_datasDatagrid.datagrid('acceptChanges');
					$.messager.show({msg:data.msg,title:'系统提示'});
					_datasDatagrid.datagrid('reload');
				} else {
					_datasDatagrid.datagrid('rejectChanges');
					$.messager.alert('错误', data.msg, 'warning');
				}
				editRowsNumber = -1;
				_datasDatagrid.datagrid('unselectAll');
			}, "json");
		}
	});

});